﻿/* 
 * Copyright 2010 Justin Pealing
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

using System;
using System.Net;
using System.Text;
using System.IO;

namespace Minnow.Web.Http
{
    /// <summary>
    /// Iplementation of <see cref="Minnow.Web.Http.IHttpRequest" /> implemented by an
    /// underlying <see cref="System.Net.HttpListenerRequest" />
    /// </summary>
    public class ListenerHttpResponse : MarshalByRefObject, IHttpResponse
    {
        private HttpListenerResponse response;

        /// <summary>
        /// Initialises a new instance of ListenerHttpResponse for a given HttpListenerResponse
        /// </summary>
        /// <param name="Response">HttpListenerResponse to wrap</param>
        /// <exception cref="System.ArgumentNullException">
        /// Response is null
        /// </exception>
        public ListenerHttpResponse(HttpListenerResponse Response)
        {
            if (Response == null)
            {
                throw new ArgumentNullException("Response is null");
            }

            this.response = Response;
        }

        #region IHttpResponse Members

        public Encoding ContentEncoding
        {
            get
            {
                return this.response.ContentEncoding;
            }
            set
            {
                this.response.ContentEncoding = value;
            }
        }

        public long ContentLength64
        {
            get
            {
                return this.response.ContentLength64;
            }
            set
            {
                this.response.ContentLength64 = value;
            }
        }

        public string ContentType
        {
            get
            {
                return this.response.ContentType;
            }
            set
            {
                this.response.ContentType = value;
            }
        }

        public bool KeepAlive
        {
            get
            {
                return this.response.KeepAlive;
            }
            set
            {
                this.response.KeepAlive = value;
            }
        }

        public Stream OutputStream
        {
            get
            {
                return this.response.OutputStream;
            }
        }

        public string RedirectLocation
        {
            get
            {
                return this.response.RedirectLocation;
            }
            set
            {
                this.response.RedirectLocation = value;
            }
        }

        public bool SendChunked
        {
            get
            {
                return this.response.SendChunked;
            }
            set
            {
                this.response.SendChunked = value;
            }
        }

        public int StatusCode
        {
            get
            {
                return this.response.StatusCode;
            }
            set
            {
                this.response.StatusCode = value;
            }
        }

        public string StatusDescription
        {
            get
            {
                return this.response.StatusDescription;
            }
            set
            {
                this.response.StatusDescription = value;
            }
        }

        public void Abort()
        {
            this.response.Abort();
        }

        public void AddHeader(string name, string value)
        {
            this.response.AddHeader(name, value);
        }

        public void AppendCookie(Cookie cookie)
        {
            this.response.AppendCookie(cookie);
        }

        public void AppendHeader(string name, string value)
        {
            if (name.ToLower() == "content-length")
            {
                this.response.ContentLength64 = long.Parse(value);
            }
            else
            {
                this.response.AppendHeader(name, value);
            }
           
        }

        public void Close()
        {
            this.response.Close();
        }

        public void Close(byte[] responseEntity, bool willBlock)
        {
            this.response.Close(responseEntity, willBlock);
        }

        public void Redirect(string url)
        {
            this.response.Redirect(url);
        }

        public void SetCookie(Cookie cookie)
        {
            this.response.SetCookie(cookie);
        }

        #endregion
    }
}
